package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.cskaoyan.market.util.ResponseUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class WxBrandServiceImpl implements WxBrandService{
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Override
    public Map<String, Object> list(MarketBrand marketBrand, int page, int limit)  {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if(marketBrand.getId()!=null){
            criteria.andIdEqualTo(marketBrand.getId());
        }
        PageHelper.startPage(page,limit);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> marketBrandPageInfo = new PageInfo<>();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",marketBrandPageInfo.getTotal());

        data.put("pages", marketBrandPageInfo.getPages());
        data.put("limit", limit);
        data.put("page", page);
        data.put("list", marketBrands);

        return (Map<String, Object>) ResponseUtil.ok(data);
    }

    @Override
    public Map<String, Object> detail(int id) {
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(id);


        if (marketBrand == null){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("errno",1);
            hashMap.put("errmsg","未找到该品牌");

        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", marketBrand.getId());
        map.put("name", marketBrand.getName());
        map.put("desc", marketBrand.getDesc());
        map.put("addTime", marketBrand.getAddTime());
        map.put("deleted", marketBrand.getDeleted());
        map.put("floorPrice", marketBrand.getFloorPrice());
        map.put("picUrl", marketBrand.getPicUrl());
        map.put("sortOrder", marketBrand.getSortOrder());
        map.put("updateTime", marketBrand.getUpdateTime());
        return (Map<String, Object>) ResponseUtil.ok(map);

    }
}
